<?php

/*
	FalconKMS Common Functions File
	Version: 1.1
	Author: Geoff Wilson (g.a.wilson at ncl.ac.uk)
	Author: Adam Tomasz Trzcinski (mail at sentryraven.de)

	Provides functions to be shared with other code
	
	------------------------------------------------------------------------------
	IMPORTANT:
	
	Functions that return ID's for user created data should automatically create
	new records if the supplied string was not found, e.g Players/Corps/Alliances.
	
	Functions that return ID's for fixed data (ships, systems etc..) should NOT
	add the values to the database, and should instead flag an error.
	
	------------------------------------------------------------------------------
	1.1
		* Add "Magic Quote" checking to all strings, required for certain items
		* Added getWeaponGraphic() function
		* Added logError() function
		* Improved Comments
	1.0-BETA
		* Initial version
*/

// Provide database connection
include_once "config.php";

function getFactionID($factionName)
{
    $factionName = ltrim(rtrim($factionName));

    // Fetch the faction ID from the database
    $sql = "SELECT (`factionID`) FROM `kb_faction` WHERE `factionName` = '$factionName'";
    $queryResult = mysql_query($sql);

    if (mysql_num_rows($queryResult) > 0) return mysql_result($queryResult,0);
    else
    {
        $sql = "INSERT INTO `kb_faction` (`factionID`, `factionName`, `filter`) VALUES (NULL, '$factionName', 0)";
        $queryResult = mysql_query($sql);

        $sql = "SELECT LAST_INSERT_ID()";
        return mysql_result($queryResult = mysql_query($sql), 0);
    }
}

function getFactionName($factionID)
{
    $sql = "SELECT (`factionName`) FROM `kb_faction` WHERE `factionID` = '$factionID' LIMIT 0,1";
    $queryResult = mysql_query($sql);

    if (mysql_num_rows($queryResult) < 1)
    {
        return 0;
    }
    else
    {
        $details = mysql_fetch_array($queryResult);
        return $details[0];
    }

}

function getAllianceID($allianceName)
{
	// Check if we need to add slashes to the alliance name string
	$allianceName = ltrim(rtrim($allianceName));
	
	// Fetch the alliance ID from the database
	$sql = "SELECT * FROM `kb_alliance` WHERE `allianceName` = '$allianceName'";
	$queryResult = mysql_query($sql);
	
	// Alliance already existis, return the ID to the client
	if (mysql_num_rows($queryResult) > 0) return mysql_result($queryResult,0);
	else
	{
		// Alliance was not found we need to create it in the database
		$sql = "INSERT INTO `kb_alliance` (`allianceID`, `allianceName`, `filter`) VALUES (NULL, '$allianceName', 0)";
		$queryResult = mysql_query($sql);
		
		// Get ID of the new alliance record and return it to the client.
		$sql = "SELECT LAST_INSERT_ID()";
		return mysql_result($queryResult = mysql_query($sql), 0);
	}
}

function getAllianceName($allianceID)
{
	// Get allaicne name from the database
	$sql = "SELECT * FROM `kb_alliance` WHERE `allianceID` = '$allianceID' LIMIT 0,1";
	$queryResult = mysql_query($sql);
	
	// Check to see if the supplied alliance ID is valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// Alliance ID was valid, return the Name to the client
		$details = mysql_fetch_array($queryResult);
		return $details[1];
	}
}

/*
	Returns the ID of the supplied corp name, requires the alliance ID.
	Alliance ID is required so a new corp record can be created if it is not present
*/
function getCorpID($corpName, $allianceID)
{
	// Check if we need to add slashes to the corp name
	$corpName = ltrim(rtrim($corpName));
	
	// Fetch the corp ID from the databse
	$sql = "SELECT * FROM `kb_corp` WHERE `corpName` = '$corpName'";
	$queryResult = mysql_query($sql);
	
	// Corp is already in database, so just return the corpID to client
	if (mysql_num_rows($queryResult) > 0)
	{
		$sql = "UPDATE `kb_corp` SET `allianceID` = '$allianceID' WHERE `corpName` = '$corpName' LIMIT 1";
		$updateResult = mysql_query($sql);
			
		return mysql_result($queryResult,0);
	}
	else
	{
		// We need to create a new corp record
		$sql = "INSERT INTO `kb_corp` (`corpID`, `corpName`, `allianceID`, `killPoints`, `lossPoints`, `netPoints`, `npcCorp`, `filter`) VALUES (NULL, '$corpName', '$allianceID', 0, 0, 0, 0, 0)";
		$queryResult = mysql_query($sql);

		// Fetch ID of the new corp record and return it the client
		$sql = "SELECT LAST_INSERT_ID()";
		return mysql_result($queryResult = mysql_query($sql), 0);
	}
}

function getCorpName($corpID)
{
	// Fetch the corp name from the databse
	$sql = "SELECT * FROM `kb_corp` WHERE `corpID` = '$corpID' LIMIT 0,1";
	$queryResult = mysql_query($sql);
	
	// Check if corp ID was valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// CorpID was valid, return name string to the client
		$details = mysql_fetch_array($queryResult);
		return $details[1];
	}
}

/*
	Returns the ID of the supplied player name, requires corp ID
	Corp ID is required so a new player record can be created if it is not already present in the database
*/
function getPlayerID($playerName, $corpID)
{
	// Check if we need to add quotes to the player name string
	$playerName = ltrim(rtrim($playerName));
	
	// Fetch player ID from the database
	$sql = "SELECT * FROM `kb_player` WHERE `playerName` = '$playerName'";
	$queryResult = mysql_query($sql);
	
	// Player ID was found, return it to the client
	if (mysql_num_rows($queryResult) > 0) 
	{
		$sql = "UPDATE `kb_player` SET `corpID` = '$corpID' WHERE `playerName` = '$playerName' LIMIT 1";
		$updateResult = mysql_query($sql);
		
		return mysql_result($queryResult,0);
	}
	else
	{
		// Player ID was not found, we need to create the player.
		$sql = "INSERT INTO `kb_player` (`playerID`, `playerName`, `killPoints`, `lossPoints`, `netPoints`, `corpID`, `password`, `canPost`, `canAdmin`, `filter`) VALUES (NULL, '$playerName', 0, 0, 0, '$corpID', NULL, 0, 0, 0)";
		$queryResult = mysql_query($sql);

		// Get new player ID
		$sql = "SELECT LAST_INSERT_ID()";
		return mysql_result($queryResult = mysql_query($sql), 0);
	}
}

function getPlayerName($playerID)
{
	// Fetch the player name from the database
	$sql = "SELECT * FROM `kb_player` WHERE `playerID` = '$playerID' LIMIT 0,1";
	$queryResult = mysql_query($sql);
	
	// Make sure playerID was valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// playerID was valid, return the player name string to the client.
		$details = mysql_fetch_array($queryResult);
		return $details[1];
	}
}

function getSolarSystemID($solarSystemName)
{
	// Check if we need to add quotes to the solar system string
	$solarSystemName = rtrim(ltrim($solarSystemName));
	
	// Fetch solar system ID from database
	$sql = "SELECT * FROM `kb_system` WHERE `solarSystemName` = '$solarSystemName'";
	$queryResult = mysql_query($sql);
	$systemID = mysql_fetch_array($queryResult);
	
	// Return the solar system ID to the client
	return $systemID["solarSystemID"];
}

function getSolarSystemName($solarSystemID)
{
	// Fetch the system name from the database
	$sql = "SELECT * FROM `kb_system` WHERE `solarSystemID` = '$solarSystemID' LIMIT 0,1";
	$queryResult = mysql_query($sql);
	
	// Check that the system ID was valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// systemID was valid, return the system name string to the client
		$details = mysql_fetch_array($queryResult);
		return $details[3];
	}
}

function getSolarSystemSecurity($solarSystemID)
{
 	// Fetch the system security from the database
	$sql = "SELECT * FROM `kb_system` WHERE `solarSystemID` = '$solarSystemID' LIMIT 0,1";
	$queryResult = mysql_query($sql);

	// Check that the system ID was valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// System ID was valid return the security valud to the client
		$details = mysql_fetch_array($queryResult);
		return $details[4];
	}
}

function getShipID($shipName)
{
	// See if we need to add slashes to ship string
	$shipName = rtrim(ltrim($shipName));
	
	// Fetch shipID from the database
	$sql = "SELECT (`shipID`) FROM `kb_ship` WHERE `shipName` = '$shipName'";
	$queryResult = mysql_query($sql);
	$shipID = mysql_fetch_array($queryResult);
	
	// Return the ship ID to the client
	return $shipID[0];
}

function getShipName($shipID)
{
	// Fetch the ship Name from the database
	$sql = "SELECT (`shipName`) FROM `kb_ship` WHERE `shipID` = '$shipID' LIMIT 0,1";
	$queryResult = mysql_query($sql);
	
	// Make sure shipID was valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// shipID was valid, return the string to the client
		$details = mysql_fetch_array($queryResult);
		return $details[0];
	}
}

function getShipGraphicID($shipID)
{
	// Fetch the ship Name from the database
	$sql = "SELECT (`graphicID`) FROM `kb_ship` WHERE `shipID` = '$shipID' LIMIT 0,1";
	$queryResult = mysql_query($sql);

	// Make sure shipID was valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// shipID was valid, return the string to the client
		$details = mysql_fetch_array($queryResult);
		return $details[0];
	}
}

function getShipClassID($shipID)
{
	// Fetch the classID from the datbase
	$sql = "SELECT (`classID`) FROM `kb_ship` WHERE `shipID` = '$shipID' LIMIT 0,1";
	$queryResult = mysql_query($sql);
	
	// Make sure supplied shipID was valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// Ship ID was valid, return the classID to the client
		$details = mysql_fetch_array($queryResult);
		return $details[0];
	}
}

function getShipClassName($classID)
{
	// Fetch the class name from the database
	$sql = "SELECT (`className`) FROM `kb_shipclass` WHERE `classID` = '$classID' LIMIT 0,1";
	$queryResult = mysql_query($sql);
	
	// Make sure supplied classID was valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// classID was valid, return class name string to client
		$details = mysql_fetch_array($queryResult);
		return $details[0];
	}
}

function getShipNameAsLink($shipID)
{
	// Fetch ship name from database
	$sql = "SELECT (`shipName`) FROM `kb_ship` WHERE `shipID` = '$shipID' LIMIT 0,1";
	$queryResult = mysql_query($sql);
	
	// Make sure shipID was valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// shipID was valid, return the HTML string to the client
		$details = mysql_fetch_array($queryResult);
		return "<a href=\"showinfo:" . getWeaponID($details[0]) . "\">" . $details[0] . "</a>";
	}

}

function getWeaponID($weaponName)
{
	// See if we need to add slashes to weapon string
	$weaponName = rtrim(ltrim($weaponName));
	
	// Fetch the ID from the database
	$sql = "SELECT * FROM `kb_type` WHERE `typeName` = '$weaponName'";
	$queryResult = mysql_query($sql);
	$weaponID = mysql_fetch_array($queryResult);

	// Return the weapon ID to the client
	return $weaponID["typeID"];
}

function getWeaponName($weaponID)
{
	// Fetch weapon name from database
	$sql = "SELECT * FROM `kb_type` WHERE `typeID` = '$weaponID' LIMIT 0,1";
	$queryResult = mysql_query($sql);

	// Was the weapon ID valid?
	if (mysql_num_rows($queryResult) < 1) return 0;
	else
	{
		// Return the weapon Name to the client
		$details = mysql_fetch_array($queryResult);
		return $details['typeName'];
	}
}

function getWeaponGraphic($weaponID)
{
	// Fetch the weapon name from the database
	$sql = "SELECT * FROM `kb_type` WHERE `typeID` = '$weaponID' LIMIT 0,1";
	$queryResult = mysql_query($sql);
	
	// Make sure supplied weaponID was valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// weaponID was valid, we need to fetch the eve graphic id now
		$details = mysql_fetch_array($queryResult);
		$graphicID = $details['graphicID'];
		$sql = "SELECT * FROM `kb_evegraphics` WHERE `graphicID` = '$graphicID' LIMIT 0,1";
		$queryResult = mysql_query($sql);
		
		// make sure there is a graphics value for the supplied id
		if (mysql_num_rows($queryResult) < 1)
		{
			return "noImage.png";
		}
		else
		{
			$details = mysql_fetch_array($queryResult);
			
			// make sure that we have the correct image
			if (file_exists("items/icon" . $details['icon'] . ".png"))
			{
				// image exists, set filename accordingly
				$fileName = "icon" . $details['icon'] . ".png";
			}
			else
			{
				// image is not present, set filename accordingly
				$fileName = "noImage.png";
			}
					
			// return the filename to the client
			return $fileName;
			
		}
	}
}

function getItemValue($itemID)
{
	// fetch the weapon name from the database
	$sql = "SELECT * FROM `kb_type` WHERE `typeID` = '$itemID' LIMIT 0,1";
	$queryResult = mysql_query($sql);
	
	// make sure supplied weaponID was valid
	if (mysql_num_rows($queryResult) < 1)
	{
		return 0;
	}
	else
	{
		// weaponID was valid, return HTML string to the client
		$details = mysql_fetch_array($queryResult);
		return $details['basePrice'];
	}
}

function logError($module, $description)
{
	// Set the date, example: 30-08-2006 23:50
	$date = date("d-m-Y H:i");
	
	// Inset error details into the datasbe
	$sql = "INSERT INTO `kb_errors` (`errorID`, `module`, `description`, `date`) VALUES (NULL, '$module', '$description', '$date')";
	$queryResult = mysql_query($sql);
	
	// if this fails, print out the mysql error for the client
	echo ("<p>" . mysql_error() . "</p>");
}

?>